/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//function loadXMLString(xmlContent)
//{
//if (window.DOMParser)
//  {
//  parser=new DOMParser();
//  xmlDoc=parser.parseFromString(xmlContent,"text/xml");
//  }
//else // Internet Explorer
//  {
//  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
//  xmlDoc.async=false;
//  xmlDoc.loadXML(xmlContent);
//  }
//return xmlDoc;
//}

function traversalDOMTree(fileName) {
    try{
        var xmlDOM = new ActiveXObject("Microsoft.XMLDOM");
        xmlDOM.async = false;
        xmlDOM.load(fileName);
        if(xmlDOM.parseError.errorCode != 0){
            alert("Error: " + xmlDOM.parseError.reason);
        }else{
            aleart(xmlDOM.documentElement.xml);
        }
    }catch(er){
        console.log(er);
    }

}

function parseXML (xmlString) {
    var dom = null;
    if (window.DOMParser) {
        try {
            dom = (new DOMParser()).parseFromString(xmlString, "text/xml");
        }
        catch (e) {
            dom = null;
        }
    } else if (window.ActiveXObject) {
        try {
            dom = new ActiveXObject('Microsoft.XMLDOM');
            dom.async = false;
            if (!dom.loadXML(xmlString)) // parse error ..
                window.alert(dom.parseError.reason + dom.parseError.srcText);
        }
        catch (e) {
            dom = null;
        }
    } else alert("cannot parse xml string!");
    return dom;
}

function loadXMLDoc(fileName)
{
    if (window.XMLHttpRequest)
    {
        xhttp=new XMLHttpRequest();
    }
    else
    {
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET",fileName,false);
    xhttp.send();
    return xhttp.responseXML;
}

/**
 * Convert list product to xml string send to server.
 */
function convertProductToXML(items){
    if(!items) {
        return "";
    }

    var product;
//    var xmlResult = '<ns4:products xmlns:ns2="dto.image" xmlns:ns3="dto.product" xmlns:ns4="collection.products">';
    var xmlResult = '<products xmlns:ns2="http://xml.netbeans.org/schema/image">';
    for (var i = 0; i < items.length; i++) {
        product = items[i].item;
        xmlResult += '<product id="' + product.id + '" >';
        xmlResult += "<name>" + product.name + "</name>";
        xmlResult += "<description>" + product.desc + "</description>";
        xmlResult += "<price>" + product.price + "</price>";
        xmlResult += "<quantity>" + items[i].amount + "</quantity>";
        xmlResult += "</product>";
    }

//    xmlResult += "</ns4:products>";
    xmlResult += "</products>";
    return xmlResult;
}

function convertItemsToXML(items) {
    if(!items || items.length == 0) {
        return "";
    }

    var product;
    var xmlResult = '<products>';
    var total = 0;
    for (var i = 0; i < items.length; i++) {
        product = items[i].item;
        xmlResult += '<product id="' + product.id +'">';
        xmlResult += "<name>" + product.name + "</name>";
        xmlResult += "<description>" + product.desc + "</description>";
        xmlResult += "<price>" + product.price + "</price>";
        xmlResult += "<quantity>" + items[i].amount + "</quantity>";
        xmlResult += "<subtotal>" + product.price * items[i].amount + "</subtotal>";        
        xmlResult += "</product>";
        total += Number(product.price * items[i].amount);
    }

    // fix charges is 10000
//    var charges = 10000;
//    total += Number(charges);
    var today = new Date();
    var formatDate = today.getDate() + "/" + (today.getMonth() + 1) + "/" + today.getFullYear();
    xmlResult += "<dateprocess>" + formatDate + "</dateprocess>";
//    xmlResult += '<charges>' + charges + '</charges>'
    xmlResult += '<total>' + total + '</total>'
    xmlResult += "</products>";
    return xmlResult;
}